import { Controller } from 'egg';

class UserController extends Controller {
    /**
     * 根据id查询用户信息
     */
    public async searchUserById() {
        const { ctx } = this;
        let param: any = {};
        if (ctx.req.method === 'GET') {
            param = ctx.query;
        } else if (ctx.req.method === 'POST') {
            param = ctx.request.body;
        }
        const userId = param.userId;
        if (!userId) {
            ctx.body = { code: 4, msg: '参数不全' };
            return;
        }
        const user = await this.service.userService.searchUserById(param.userId);
        ctx.body = { code: 1, msg: '查询成功', data: user[0] };
    }

    /**
     * 根据条件查询用户列表
     */
    public async searchUserListByParam() {
        const { ctx } = this;
        let param: any = {};
        param = ctx.request.body;
        const page = Number(param.page);
        const size = Number(param.size);
        if (!page || !size) {
            ctx.body = ctx.helper.responseErrorParam('请输入正确的页码和页数');
            return;
        }
        const actualParam: any = {};
        for (const i in param) {
            if (i === 'page' || i === 'size' || !param[i]) {
                continue;
            }
            actualParam[i] = param[i];
        }
        actualParam.is_delete = '1';
        const userList = await this.service.userService.searchUserAll(page, size, actualParam);
        ctx.body = { code: 1, msg: '查询成功', data: userList };
    }

    /**
     * 操作用户状态
     */
    public async editUserStatus() {
        const { ctx } = this;
        let param = ctx.request.body;
        let type = param.type;
        const user_id = param.user_id;
        if (!type || !user_id) {
            ctx.body = { code: 4, msg: '参数不全' };
            return;
        }
        type = Number(type);
        if (typeof (type) !== "number") {
            ctx.body = { code: 4, msg: '操作类型请输入数字' };
            return;
        }
        if (type !== 1 && type !== 2 && type !== 3 && type !== 4) {
            ctx.body = { code: 4, msg: '无法识别的操作类型' };
            return;
        }
        if (type === 3) {
            let anexcuse_time = Number(param.anexcuse_time);
            if (!anexcuse_time) {
                throw new Error(JSON.stringify({ code: 4, msg: '禁言时长必须为正整数' }));
            }
        }
        await this.service.userService.editUserStatus(param);
        ctx.body = { code: 1, msg: '操作成功' };
    }

    /**
     * 获取每日注册量
     */
    public async getRegisterNum() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                type: 'type',
                startTime: 'string',
                endTime: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }

        const result = await ctx.service.userService.getRegisterNum(request.body);
        ctx.body = ctx.helper.responseSuccess('操作成功', result);
    }

    /**
     * 微信端用户登录
     */
    public async wxLogin() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                user_phone: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.userService.wxLogin(request.body);
        ctx.body = ctx.helper.responseSuccess('登录成功', result);
    }

    /**
     * 用户注册
     */
    public async userRegister() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                user_nickname: 'string',
                user_phone: 'string',
                user_code: 'string',
                user_sex: 'string',
                user_school_id: 'string',
                user_college_id: 'string',
                user_school_year: 'string',
                user_student_id: 'string',
                user_school_Card: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.userService.userRegister(request.body);
        ctx.body = ctx.helper.responseSuccess('注册成功，等待管理员审核', result);
    }

    /**
     * 发送验证码
     */
    public async sendSMS() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                phone: 'string',
                type: 'type'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result: any = await ctx.service.userService.sendSMS(request.body);
        const { code } = result;
        const returnData: any = {};
        if (code == 2) {
            returnData.code = 2;
            returnData.msg = result.msg.message;
        } else {
            returnData.code = result.code;
            returnData.msg = result.msg;
        }
        ctx.body = returnData;
    }

    /**
     * 根据id获取个人主页信息
     */
    public async searchUserPageById() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                type: 'type',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const type=request.body.type;
        const user_id=request.body.user_id;
        const target_user_id=request.body.target_user_id;
        if(type==1&&!target_user_id){
            ctx.body = ctx.helper.responseErrorParam('target_user_id is request');
            return;
        }
        if(type==2&&!user_id){
            ctx.body = ctx.helper.responseErrorParam('user_id is request');
            return;
        }
        let result = await ctx.service.userService.searchUserPageById(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功', result);
    }

    /**
     * 修改新用户为老用户
     */
    public async updateUserNew() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                user_id: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.userService.updateUserNew(request.body);
        ctx.body = ctx.helper.responseSuccess('修改新用户为老用户成功');
    }

    /**
     * 举报用户
     */
    public async userReport() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                report_user_id: 'string',
                accusation_reason: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.userService.userReport(request.body);
        ctx.body = ctx.helper.responseSuccess('举报成功');
    }

    /**
     * 屏蔽用户
     */
    public async shieldUser() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                user_id: 'string',
                target_user_id: 'string',
                type: 'type'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const user_id = request.body.user_id;
        const target_user_id = request.body.target_user_id;
        if (user_id == target_user_id) {
            ctx.body = ctx.helper.responseErrorMsg('不能屏蔽本人');
            return;
        }
        await ctx.service.userService.shieldUser(request.body);
        ctx.body = ctx.helper.responseSuccess('操作成功');
    }

    /**
     * 拉黑用户
     */
    public async defriend() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                user_id: 'string',
                target_user_id: 'string',
                type: 'type'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const user_id = request.body.user_id;
        const target_user_id = request.body.target_user_id;
        if (user_id == target_user_id) {
            ctx.body = ctx.helper.responseErrorMsg('不能拉黑本人');
            return;
        }
        await ctx.service.userService.defriend(request.body);
        ctx.body = ctx.helper.responseSuccess('操作成功');
    }

    /**
     * 惦记用户
     */
    public async makeFriends() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                user_id: 'string',
                target_user_id: 'string',
                type: 'type'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const user_id = request.body.user_id;
        const target_user_id = request.body.target_user_id;
        if (user_id == target_user_id) {
            ctx.body = ctx.helper.responseErrorMsg('不能惦记本人');
            return;
        }
        let result = await ctx.service.userService.makeFriends(request.body);
        if (!result) {
            result = {};
        }
        ctx.body = ctx.helper.responseSuccess('操作成功', result);
    }

    /**
     * 判断当前用户与目标用户的关系
     */
    public async checkRelation() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                target_user_id: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.userService.checkRelation(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功', result);
    }

    /**
     * 编辑用户资料
     */
    public async editUser() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.userService.editUser(request.body);
        ctx.body = ctx.helper.responseSuccess('编辑成功');
    }

    /**
     * 编辑评价状态
     */
    public async editEvaluate() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                evaluate_id: 'string',
                type: 'type',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.userService.editEvaluate(request.body);
        ctx.body = ctx.helper.responseSuccess('编辑成功');
    }

    /**
     * 评价用户
     */
    public async evaluateUser() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                target_user_id: 'string',
                evaluate_content: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.userService.evaluateUser(request.body);
        ctx.body = ctx.helper.responseSuccess('评价成功');
    }

    /**
     * 根据用户名或关键词查找用户
     */
    public async searchUserByNameOrKey() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                page: 'string',
                size: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const user_id = request.body.user_id;
        const school_id = request.body.school_id;
        if (user_id && user_id.length > 0) {
            if (!school_id) {
                ctx.body = ctx.helper.responseErrorParam('学校id不能为空');
                return;
            }
        }
        const result = await ctx.service.userService.searchUserByNameOrKey(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功', result);
    }

    /**
     * 根据条件和搜索次数查找用户
     */
    public async searchUserByParamAndNum() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                page: 'string',
                size: 'string',
                param: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const user_id = request.body.user_id;
        const school_id = request.body.school_id;
        if (user_id && user_id.length > 0) {
            if (!school_id) {
                ctx.body = ctx.helper.responseErrorParam('学校id不能为空');
                return;
            }
        }
        const result = await ctx.service.userService.searchUserByParamAndNum(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功', result);
    }

    /**
     * 普通url转公众号授权url
     */
    public async getAuthorizeUrl() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                url: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.userService.getAuthorizeUrl(request.body);
        ctx.body = ctx.helper.responseSuccess('转换成功', result);
    }

    /**
     * 根据code换取openid
     */
    public async getopenId() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                code: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.userService.getopenId(request.body);
        ctx.body = ctx.helper.responseSuccess('获取成功', result);
    }

    /**
     * 根据openId判断是否注册（如果已注册则返回用户id，学校id等信息）
     */
    public async checkRegisterByOpenId() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                openId: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.userService.checkRegisterByOpenId(request.body);
        ctx.body = ctx.helper.responseSuccess('获取成功', result);
    }

    /**
     * 增加图片
     */
    public async createPicture() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                picture_name: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.userService.createPicture(request.body);
        ctx.body = ctx.helper.responseSuccess('添加成功');
    }

    /**
     * 删除图片
     */
    public async deletePicture() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                picture_id: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.userService.deletePicture(request.body);
        ctx.body = ctx.helper.responseSuccess('删除成功');
    }

    /**
     * 查找图片列表
     */
    public async searchPicture() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                page: 'string',
                size: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const page = Number(request.body.page);
        const size = Number(request.body.size);
        if (!page || page <= 0) {
            ctx.body = ctx.helper.responseErrorParam('页码输入错误');
            return;
        }
        if (!size || size <= 0) {
            ctx.body = ctx.helper.responseErrorParam('一页显示数输入错误');
            return;
        }
        const result = await ctx.service.userService.searchPicture(request.body);
        ctx.body = ctx.helper.responseSuccess('查找成功', result);
    }

    /**
     * 获取当前用户状态
     */
    public async getUserStatus() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.userService.getUserStatus(request.body);
        ctx.body = ctx.helper.responseSuccess('查找成功', result);
    }

    /**
     * 重定向到微信授权
     */
    public async directToGetCode() {
        const { ctx } = this;
        const activity_id = ctx.query.activity_id || '';

        await this.ctx.service.userService.directToGetCode(activity_id);
    }

    /**
     * 微信服务器重定向地址（获取code）
     */
    public async getCode() {
        const { ctx } = this;
        await ctx.service.userService.getCode(ctx.query);
    }

    /**
     * 根据用户token获取融云token
     */
    public async getCloundTokenByUserToken() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                user_token: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.userService.getCloundTokenByUserToken(request.body);
        ctx.body = ctx.helper.responseSuccess('查找成功', result);
    }

    /**
     * 获取token
     */
    public async getCloundToken() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.axiosService.getCloundToken(request.body);
        ctx.body = ctx.helper.responseSuccess('查找成功', result);
    }

    /**
     * 更新搜索结果
     */
    public async updateResultList() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                user_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.userService.updateResultList(request.body);
        ctx.body = ctx.helper.responseSuccess('更新成功', result);
    }
}

export default UserController;
